package com.zlt.miaosha.controller;

import com.zlt.miaosha.domain.MiaoshaUser;
import com.zlt.miaosha.redis.GoodsKey;
import com.zlt.miaosha.redis.RedisService;
import com.zlt.miaosha.result.Result;
import com.zlt.miaosha.service.GoodsService;
import com.zlt.miaosha.service.MiaoshaUserService;
import com.zlt.miaosha.vo.GoodsDetailVo;
import com.zlt.miaosha.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zhoulitong
 * @date 2018/6/14
 * @description
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    MiaoshaUserService userService;
    @Autowired
    RedisService redisService;
    @Autowired
    GoodsService goodsService;
    /**
     * 页面缓存处理
     */
    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;
    /**
     * 页面缓存处理
     */
    @Autowired
    ApplicationContext applicationContext;


    /**
     * 查询商品列表
     * @param model
     * @param user
     * @return
     */
    @GetMapping(value = "/to_list",produces = "text/html")
    @ResponseBody
    public String list(HttpServletRequest request,HttpServletResponse response,Model model, MiaoshaUser user){
        model.addAttribute("user", user);
        //查询商品列表
        List<GoodsVo> goodsVoList = goodsService.listGoodsVo();
        model.addAttribute("goodsList", goodsVoList);
        //todo 页面缓存处理
        //取缓存
        String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
        if (!StringUtils.isEmpty(html)) {
            return html;
        }
        SpringWebContext ctx = new SpringWebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);
        //手动渲染
        html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);
        if (!StringUtils.isEmpty(html)) {
            redisService.set(GoodsKey.getGoodsList, "",html);
        }
        return html;
    }

//    @GetMapping(value = "/to_detail/{goodsId}", produces = "text/html")
//    @ResponseBody
//    public String detail2(HttpServletRequest request,HttpServletResponse response,Model model, MiaoshaUser user, @PathVariable("goodsId")long goodsId){
//        model.addAttribute("user", user);
//        //取缓存
//        String html = redisService.get(GoodsKey.getGoodsDetail, ""+goodsId, String.class);
//        if (!StringUtils.isEmpty(html)) {
//            return html;
//        }
//        //手动渲染
//        GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
//        model.addAttribute("goods", goods);
//
//        //秒杀时间进行处理
//        long startAt = goods.getStartDate().getTime();
//        long endAt = goods.getEndDate().getTime();
//        long now = System.currentTimeMillis();
//
//        int miaoshaStatus = 0;
//        int remainSeconds = 0;
//
//        //秒杀还没开始，倒计时
//        if (now < startAt) {
//            miaoshaStatus = 0;
//            remainSeconds = (int) ((startAt-now)/1000);
//        }
//        //秒杀已经结束
//        else if (now > endAt) {
//            miaoshaStatus = 2;
//            remainSeconds = -1;
//        }
//        //秒杀进行中
//        else {
//            miaoshaStatus =1;
//            remainSeconds =0;
//        }
//
//        model.addAttribute("miaoshaStatus", miaoshaStatus);
//        model.addAttribute("remainSeconds", remainSeconds);
//        //return "goods_detail";
//        SpringWebContext ctx = new SpringWebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);
//        //手动渲染
//        html = thymeleafViewResolver.getTemplateEngine().process("goods_detail", ctx);
//        if (!StringUtils.isEmpty(html)) {
//            //拼接goodsId，不同的物品有不同的详情
//            redisService.set(GoodsKey.getGoodsDetail, ""+goodsId,html);
//        }
//        return html;
//    }

    @GetMapping(value="/detail/{goodsId}")
    @ResponseBody
    public Result<GoodsDetailVo> detail(HttpServletRequest request,HttpServletResponse response,Model model, MiaoshaUser user, @PathVariable("goodsId")long goodsId){
        GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);

        //秒杀时间进行处理
        long startAt = goods.getStartDate().getTime();
        long endAt = goods.getEndDate().getTime();
        long now = System.currentTimeMillis();

        int miaoshaStatus = 0;
        int remainSeconds = 0;

        //秒杀还没开始，倒计时
        if (now < startAt) {
            miaoshaStatus = 0;
            remainSeconds = (int) ((startAt-now)/1000);
        }
        //秒杀已经结束
        else if (now > endAt) {
            miaoshaStatus = 2;
            remainSeconds = -1;
        }
        //秒杀进行中
        else {
            miaoshaStatus =1;
            remainSeconds =0;
        }

        GoodsDetailVo vo = new GoodsDetailVo();
        vo.setGoods(goods);
        vo.setUser(user);
        vo.setRemainSeconds(remainSeconds);
        vo.setMiaoshaStatus(miaoshaStatus);
        return Result.success(vo);
    }
}
